package com.jd.ty.web;

import java.util.List;

import com.jd.ty.pojo.Customer;
import com.jd.ty.pojo.CustomerCompany;
import com.jd.ty.pojo.Dictionary;
import com.jd.ty.pojo.Function;
import com.jd.ty.service.CustomerCompanyService;
import com.jd.ty.service.DictionaryService;
import com.jd.ty.service.FunctionService;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.Pager;
import com.jd.ty.web.constant.DictionaryCode;
import com.jd.ty.web.filter.CustomerCompanyFilter;

public class CustomerCompanyAction extends BaseAction {
	private static final long serialVersionUID = -4018571604337249963L;
	private Long id;

	private CustomerCompanyFilter filter;

	private CustomerCompanyService customerCompanyService;

	private CustomerCompany customerCompany = new CustomerCompany();

	private List<Dictionary> sexs;
	private List<Dictionary> titles;
	private DictionaryService dictionaryService;
	private List<Customer> customers;
	private Customer customer;
	public String customerDetail(){
		customer=customerCompanyService.getCustomer(id);
		return SUCCESS;
	}
	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public List<Dictionary> getSexs() {
		return sexs;
	}

	public void setSexs(List<Dictionary> sexs) {
		this.sexs = sexs;
	}

	public List<Dictionary> getTitles() {
		return titles;
	}

	public void setTitles(List<Dictionary> titles) {
		this.titles = titles;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String customerList() {
		pager = getPagerObject();
		List<Customer> res = (List<Customer>) customerCompanyService.getCustomersByCompanyId(id);
		pager.setDataRows(res);
		return SUCCESS;

	}
	public String customerCompanyList() {
		pager = getPagerObject();
		List<CustomerCompany> res = (List<CustomerCompany>) customerCompanyService.getList(pager,
				getFilter().toHqlString(), getSortStr());
		pager.setDataRows(res);
		return SUCCESS;

	}
	public String customerCompanyInfo() {
		if (id != null) {
			customerCompany = customerCompanyService.get(id);
		}
		loadResources();
		return SUCCESS;
	}

	public String customerCompanyUpdate() throws ServiceException {
		try {
			if (customerCompany.getCustCompanyId() == null) {
				customerCompanyService.add(customerCompany);
			} else {
				customerCompanyService.update(customerCompany);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			customerCompanyService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}

	public void setFilter(CustomerCompanyFilter filter) {
		this.filter = filter;
	}

	public CustomerCompanyService getCustomerCompanyService() {
		return customerCompanyService;
	}

	public void setCustomerCompanyService(CustomerCompanyService customerCompanyService) {
		this.customerCompanyService = customerCompanyService;
	
	}

	public CustomerCompany getCustomerCompany() {
		return customerCompany;
	}

	public void setCustomerCompany(CustomerCompany customerCompany) {
		this.customerCompany = customerCompany;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {

		this.page = page;

	}

	public CustomerCompanyFilter getFilter() {
		if (filter == null) {
			filter = new CustomerCompanyFilter();
		}

		return filter;
	}

	private void loadResources() {
		sexs = dictionaryService.getList(DictionaryCode.SEX);
		titles = dictionaryService.getList(DictionaryCode.CUSTOMER_TITLE);
        customers=customerCompanyService.getCustomersByCompanyId(id);
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Customer getCustomer() {
		return customer;
	}
}
